<template>
  <div class="login-container">
    <!--
      element-plus
        el-form是element-plus提供表单组件
          model:代表表单元素收集的数据收集到哪里
          rules:代表的是表单的校验规则
    -->
    <el-form
      ref="formRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <!--登录页面的标题-->
      <div class="title-container">
        <h3 class="title">尚品汇后台管理</h3>
      </div>
      <!--form的第一个表单项-->
      <el-form-item prop="username">
        <!--账号前面的图标组件-->
        <span class="svg-container">
          <svg-icon name="ele-OfficeBuilding" />
        </span>
        <!--输入账号的表单元素组件-->
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="请你输入账号"
          name="username"
        />
      </el-form-item>
      <!--form的第二个表单项-->
      <el-form-item prop="password">
        <!--密码前面的锁头-->
        <span class="svg-container">
          <svg-icon name="ele-Lock" />
        </span>
        <!--输入密码的表单元素-->
        <el-input ref="passwordRef" v-model="loginForm.password" :type="passwordType" placeholder="请你输入密码" @keyup.enter="handleLogin"/>
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :name="passwordType === 'password' ? 'ele-Hide' : 'ele-View'"
          />
        </span>
      </el-form-item>
      <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px; height: 40px" @click.prevent="handleLogin">登 陆</el-button>
    </el-form>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import { nextTick, ref, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
//路由
const route = useRoute();
//路由器
const router = useRouter();
//引入大仓库里面的userInfoStore小仓库
import { useUserInfoStore } from "@/stores/userInfo";
//小仓库
const userInfoStore = useUserInfoStore();
//控组登录按钮是否有加载效果
const loading = ref(false);
//密码的组件的VC
const passwordRef = ref<HTMLInputElement>();
//获取el-form表单组件实例
const formRef = ref<FormInstance>();
//收集表单数据:账号与密码
const loginForm = ref({
  username: "admin",
  password: "111111",
});

//校验规则
const validateUsername = (rule: any, value: any, callback: any) => {
  //rule:校验规则对象
  //value:用户输入进来的表单元素文本内容
  //callBack,函数，校验规则通过调用次函数,如果校验规则没通过，返回错误提示信息
  if (value.length >= 5) {
    //如果用户名字大于等于六位负责规则，没有错误提示信息
    callback();
  } else {
    callback(new Error("请保证用户名大于等于五位"));
  }
};
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value.length < 6) {
    callback(new Error("密码长度不能小于6位"));
  } else {
    callback();
  }
};
//表单校验:校验用户名与密码规则
//分别校验用户名与密码
const loginRules = {
  //数组里面每一个对象:代表一个校验规则
  //required:代表用户名务必校验
  //validator:校验规则
  //trigger:触发校验规则的实际 change(文本发生变化)|blur(失却焦点)
  username: [
    { required: true, validator: validateUsername, trigger: "change" },
  ],
  password: [{ required: true, trigger: "blur", validator: validatePassword }],
};
//密码的表单元素类型的响应式数据
const passwordType = ref("password");
//点击密码后面的小眼睛:切换图标
const showPwd = () => {
  passwordType.value = passwordType.value === "password" ? "text" : "password";
  //点击密码的后面眼睛:进行表单元素(密码)类型的切换
  nextTick(() => {
    //让密码框聚焦
    passwordRef.value?.focus();
  });
};

//存储地址栏query参数
const redirect = ref("");

//监听路由变化:存储query参数
watch(
  route,
  () => {
    redirect.value = route.query && (route.query.redirect as string);
  },
  { immediate: true }
);


//登录的回调
const handleLogin = async () => {
  //查看全部表单元素是否都符合规则
  await formRef.value?.validate();
  //控制登录按钮的加载效果
  loading.value = true;
  //获取账号与密码数据:发登录请求
  const { username, password } = loginForm.value;
  try {
    //点击登录按钮:在小仓库里面调用login方法进行发登录请求
    await userInfoStore.login(username, password);
    //如果登录成功:首页跳转|调到曾经想去而没有去成路由
    router.push({ path: redirect.value || "/" }); // 只要路由发生变化就会走守卫,接下来应该看守卫的内容
  } finally {
    //loading效果消息
    loading.value = false;
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: rgb(179, 118, 118);
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {
  background-image: url(../../assets/bg.jpg);
  background-color: #2d3a4b;
  background-size: cover;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    .el-input__wrapper {
      width: 100%;
      background-color: transparent;
      box-shadow: none;
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$color: yellowgreen;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $color;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 10px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
